package com.erp.quertz.emailQuertz;

import com.erp.erp_servers.Email.Impl.EmailServiceImpl;
import com.erp.erp_ui.email.EmailControl;
import com.erp.erp_ui.email.EmailUnit;
import com.erp.quertz.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.nntp.Threadable;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.mail.MessagingException;

/**
 * Spring 整合 Quartz 的  Hello World!
 * @author dufy
 * @date 2017.02.26
 * 定时拉取邮件的主信息
 */
@Slf4j
public class EmailGet implements Job  {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        /*ImpCustomerService customerService = SpringUtil.getBean(ImpCustomerService.class);
        customerService.getCustomerAndUserAndSupplier();*/

       /* System.out.println("This is a first spring combine quartz !");
        System.out.println("Welcome to Spring_Quartz World!"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
        System.out.println("Let's begin ! \n \n");*/
       try {
           long statim=System.currentTimeMillis();
           System.out.println("**************************收件线程启动"+ EmailUnit.getNowSqlDate() +"启动"+statim+"****************************");
           T1 t1=new T1();
           t1.start();
           boolean tf=true;
           while (tf){
               long end=System.currentTimeMillis()-statim;
               if( end<=20000){
                   if(t1.x==1){
                       tf=false;
                   }
               }else {
                   tf=false;
               }
           }
           //结束子线程t1
           if(t1.isAlive()){
               t1.interrupt();
           }
           System.out.println("**************************收件线程结束"+ EmailUnit.getNowSqlDate() +"耗时"+(System.currentTimeMillis()-statim)+"***************************");

       }catch (Exception e){
           System.out.println("收件多线程故障:"+e);
       }
    }
}

class T1 extends Thread implements Runnable{
    public int x=0;
    /**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see #start()
     * @see #stop()
     * @see # (ThreadGroup, Runnable, String)
     */
    @Override
    public void run() {
        //下载邮件 x=1时执行结束
        x=-1;
        try {
            EmailServiceImpl email= SpringUtil.getBean(EmailServiceImpl.class);
            get g=get.getget();
            x= g.get(email);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("中断");
        }

    }
}
